package com.astrob.view;

import java.text.SimpleDateFormat;
import java.util.Date;

import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.astrob.NavFrameSDK;
import com.astrob.NavFrameSDK.NavInfo;
import com.astrob.NavFrameSDK.NavPosition;
import com.astrob.model.LonLat;
import com.astrob.naviframe.Start;
import com.astrob.nhcmap.R;
import com.astrob.util.SysUtils;

public class NaviInfoView extends RelativeLayout {

	public ImageView mTurn; // 转弯图示
	private TextView mCurroadname; // 当前道路
	private TextView mToroadname; // 当前道路
	private TextView mDistance; // 距离下一转弯距离
	private TextView mTDistance; // 总距离
	private TextView mTTime; // 总到达时间

	Context _context;

	public NaviInfoView(Context context, AttributeSet attrs) {
		super(context, attrs);
		_context = context;
		initialViews();
	}

	public NaviInfoView(Context context) {
		super(context);
		_context = context;
		initialViews();
	}

	private void initialViews() {
		LayoutInflater inflate = (LayoutInflater) getContext()
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		inflate.inflate(R.layout.view_naviinfo, this, true);

		mTurn = (ImageView) findViewById(R.id.id_iv_turnicon);
		mToroadname = (TextView) findViewById(R.id.id_tv_toroad);
		mCurroadname = (TextView) findViewById(R.id.id_tv_curroad);
		mTDistance = (TextView) findViewById(R.id.id_tv_toldistance);
		mDistance = (TextView) findViewById(R.id.id_iv_turndis);
		mTTime = (TextView) findViewById(R.id.id_tv_toltime);	
		mTTime = (TextView) findViewById(R.id.id_tv_toltime);
		
		mDistance.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View view) {		
			}		
		});
		mTTime.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View view) {		
			}		
		});
		mTDistance.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View view) {		
			}		
		});
		
		mToroadname.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View view) {		
			}		
		});
		mCurroadname.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View view) {		
			}		
		});
		
		setHavepath(false);
	}

	public void show() {
		setVisibility(View.VISIBLE);
	}

	public void hide() {
		setVisibility(View.INVISIBLE);
	}

	String strArrivalTime;
	private NavInfo mNavInfo = null;
	private boolean havepath_ = false;
	
	public void setHavepath(boolean havepath) {
		havepath_ = havepath;
		
		if (havepath) {
//			mTurn.setVisibility(View.VISIBLE);
//			mToroadname.setVisibility(View.VISIBLE);
//			mTDistance.setVisibility(View.VISIBLE);
//			mDistance.setVisibility(View.VISIBLE);
//			mTTime.setVisibility(View.VISIBLE);
			setVisibility(View.VISIBLE);
		} else {
//			mTurn.setVisibility(View.GONE);
//			mToroadname.setVisibility(View.GONE);
//			mTDistance.setVisibility(View.GONE);
//			mDistance.setVisibility(View.GONE);
//			mTTime.setVisibility(View.GONE);
			setVisibility(View.GONE);

			if (mTDistance != null) {
				mTDistance.setText("");
				mDistance.setText("");
				mTTime.setText("");
				mToroadname.setText("");
				mTurn.setBackgroundResource(R.color.transparent);
			}
		}		
	}

	public void setOnClickListener(@Nullable OnClickListener l) {
//		overNavibtn.setOnClickListener(l);
	}
	
	public void setNavInfo(NavInfo navInfo, NavPosition navpos) {	
		mNavInfo = navInfo;		
		
		String fromRoadName = "";
		if (!havepath_) {			
			if (mNavInfo.guidInfo.fromRoadName != null) {
				fromRoadName = mNavInfo.guidInfo.fromRoadName;
			}
			if (fromRoadName.length() < 1) {
				LonLat mapPt = new LonLat(navpos.pos.lon, navpos.pos.lat);			
				fromRoadName = Start.getInstance().MapKit().getRoadnamebypos(mapPt);
			}
			
			if (fromRoadName.length() < 1) {
				fromRoadName = _context.getString(R.string.wmdl);
			}
			mCurroadname.setText(fromRoadName);
			return;
		}
		
		// 简单显示地铁信息
		if (mNavInfo.turnInfo.nCrossCount == 666) {
			mCurroadname.setText("");
			mToroadname.setText("正在搭乘地铁");
			mTDistance.setText("");
			mDistance.setText("");
			mTTime.setText("");
			mTurn.setBackgroundResource(R.drawable.turn_subway1);
			return;
		}
		
		if (mNavInfo.guidInfo.remainDistance == 0)
			return;

		fromRoadName = mNavInfo.guidInfo.fromRoadName;
		if (fromRoadName.length() > 20) {
			fromRoadName = fromRoadName.substring(0, 20) + "...";
		}
		
		if (fromRoadName.length() < 1) {
			fromRoadName = _context.getString(R.string.wmdl);
		}
		mCurroadname.setText(fromRoadName);
		String toRoadname = mNavInfo.turnInfo.toRoadName;
		if (toRoadname.contains(_context.getString(R.string.mdd))) {
			toRoadname = _context.getString(R.string.ddmddfj);
		}
		
		if (toRoadname.length() < 1) {
			toRoadname = _context.getString(R.string.wmdl);
		}
		if (toRoadname.length() > 20) {
			toRoadname = toRoadname.substring(0, 20) + "...";
		}
		mToroadname.setText(toRoadname);

		int distance = (int) mNavInfo.guidInfo.distanceToTurn;
		String DIS[] = SysUtils.getInstance().getDistanceStr(distance);
		mDistance.setText(DIS[0] + DIS[1]);

		// 设置到达时间
		double speedcal = 40.0;
		if (speedcal < navpos.vel) {
			speedcal = navpos.vel;
		}

		// km/h - > m/s
		speedcal = speedcal * 1000 / 3600;

		if (!Start.getInstance().MapKit().isCarRouteOption()) {
			// 步行速度 3 米/秒
			speedcal = 3;
		}
		long ArrivalTime = (long) (mNavInfo.guidInfo.remainDistance / speedcal); // s
		ArrivalTime = ArrivalTime * 1000 + System.currentTimeMillis();
		SimpleDateFormat formatter = new SimpleDateFormat("HH:mm");
		Date date = new Date(ArrivalTime);
		strArrivalTime = formatter.format(date);
		mTTime.setText(strArrivalTime);

		distance = (int) mNavInfo.guidInfo.remainDistance;
		String DISA[] = SysUtils.getInstance().getDistanceStr(distance);
		mTDistance.setText(DISA[0]+DISA[1]);

		boolean haveTrueDir = false;
		if (mNavInfo.turnInfo.dirType == NavFrameSDK.TurnDirection.TURN_STRAIGHT_SAME) {
			mTurn.setBackgroundResource(R.drawable.turn_straight);
		} else if (mNavInfo.turnInfo.dirType == NavFrameSDK.TurnDirection.TURN_UTURN) {
//			if (Start.getInstance().MapKit().mSelectedCountryID.compareToIgnoreCase("HKG") == 0
//					|| "CN".compareToIgnoreCase("SGP") == 0
//					|| "CN".compareToIgnoreCase("Tha") == 0
//					|| "CN".compareToIgnoreCase("MAC") == 0) {
//				mTurn.setBackgroundResource(R.drawable.turn_uturnright);
//			} else {
//				mTurn.setBackgroundResource(R.drawable.turn_uturnleft);
//			}	
			mTurn.setBackgroundResource(R.drawable.turn_uturnleft);
			haveTrueDir = true;
		} else if (mNavInfo.turnInfo.dirType == NavFrameSDK.TurnDirection.TURN_ONLYLEFT) {
			mTurn.setBackgroundResource(R.drawable.turn_keepleft);
		} else if (mNavInfo.turnInfo.dirType == NavFrameSDK.TurnDirection.TURN_ONLYRIGHT) {
			mTurn.setBackgroundResource(R.drawable.turn_keepright);
		} else if (mNavInfo.turnInfo.dirType == NavFrameSDK.TurnDirection.TURN_LEFTBEAR) {
			mTurn.setBackgroundResource(R.drawable.turn_bearleft);
			haveTrueDir = true;
		} else if (mNavInfo.turnInfo.dirType == NavFrameSDK.TurnDirection.TURN_RIGHTBEAR) {
			mTurn.setBackgroundResource(R.drawable.turn_bearright);
			haveTrueDir = true;
		} else if (mNavInfo.turnInfo.dirType == NavFrameSDK.TurnDirection.TURN_LEFT) {
			mTurn.setBackgroundResource(R.drawable.turn_turnleft);
			haveTrueDir = true;
		} else if (mNavInfo.turnInfo.dirType == NavFrameSDK.TurnDirection.TURN_RIGHT) {
			mTurn.setBackgroundResource(R.drawable.turn_turnright);
			haveTrueDir = true;
		} else if (mNavInfo.turnInfo.dirType == NavFrameSDK.TurnDirection.TURN_LEFTFRONT) {
			mTurn.setBackgroundResource(R.drawable.turn_turnleft);
		} else if (mNavInfo.turnInfo.dirType == NavFrameSDK.TurnDirection.TURN_RIGHTFRONT) {
			mTurn.setBackgroundResource(R.drawable.turn_turnright);
		} else if (mNavInfo.turnInfo.dirType == NavFrameSDK.TurnDirection.TURN_END) {
			mTurn.setBackgroundResource(R.drawable.turn_arriveend);
			haveTrueDir = true;
		} else if (mNavInfo.turnInfo.dirType == NavFrameSDK.TurnDirection.TURN_3FORK) {
			mTurn.setBackgroundResource(R.drawable.turn_keepstraight);
		} else if (mNavInfo.turnInfo.dirType == NavFrameSDK.TurnDirection.TURN_3FORKONLYLEFT) {
			mTurn.setBackgroundResource(R.drawable.turn_keepleft2);
			haveTrueDir = true;
		} else if (mNavInfo.turnInfo.dirType == NavFrameSDK.TurnDirection.TURN_3FORKONLYRIGHT) {
			mTurn.setBackgroundResource(R.drawable.turn_keepright2);
			haveTrueDir = true;
		}

		if (mNavInfo.turnInfo.categoryChange == NavFrameSDK.CategoryChangeType.TURN_MAIN2AUX) {
			if (!haveTrueDir) {
				mTurn.setBackgroundResource(R.drawable.turn_enterserviceroad);
			}
		} else if (mNavInfo.turnInfo.categoryChange == NavFrameSDK.CategoryChangeType.TURN_AUX2MAIN) {
			if (!haveTrueDir) {
				mTurn.setBackgroundResource(R.drawable.turn_leaveserviceroad);
			}
		} else if (mNavInfo.turnInfo.categoryChange == NavFrameSDK.CategoryChangeType.TURN_ENTERRAMP) {
			// mTurn.setBackgroundResource(R.drawable.turn_enterramp);
		} else if (mNavInfo.turnInfo.categoryChange == NavFrameSDK.CategoryChangeType.TURN_ENTERHWY) {
			mTurn.setBackgroundResource(R.drawable.turn_freeway);
		} else if (mNavInfo.turnInfo.categoryChange == NavFrameSDK.CategoryChangeType.TURN_EXITFERRY) {
			mTurn.setBackgroundResource(R.drawable.turn_leaveferry);
		} else if (mNavInfo.turnInfo.categoryChange == NavFrameSDK.CategoryChangeType.TURN_ENTERSERVICES) {
			mTurn.setBackgroundResource(R.drawable.turn_enterservice);
		} else if (mNavInfo.turnInfo.categoryChange == NavFrameSDK.CategoryChangeType.TURN_EXITHWY) {
			// mTurn.setBackgroundResource(R.drawable.turn_leaveramp);
		} else if (mNavInfo.turnInfo.categoryChange == NavFrameSDK.CategoryChangeType.TURN_TOLL) {
			mTurn.setBackgroundResource(R.drawable.turn_toll);
		} else if (mNavInfo.turnInfo.categoryChange == NavFrameSDK.CategoryChangeType.TURN_ENTERFERRY) {
			mTurn.setBackgroundResource(R.drawable.turn_enterferry);
		} else if (mNavInfo.turnInfo.categoryChange == NavFrameSDK.CategoryChangeType.TURN_ENTERTUNNEL) {
			mTurn.setBackgroundResource(R.drawable.turn_tunnel);
		} else if (mNavInfo.turnInfo.categoryChange == NavFrameSDK.CategoryChangeType.TURN_ENTERCIRCLE) {
			mTurn.setBackgroundResource(R.drawable.turn_roundabout);
		} else if (mNavInfo.turnInfo.categoryChange == NavFrameSDK.CategoryChangeType.TURN_EXITCIRCLE) {
			mTurn.setBackgroundResource(R.drawable.turn_roundabout);
		}
	}

}
